﻿using System;
using System.Linq;
using System.Linq.Expressions;
using System.Linq.Dynamic;using System.Text;
using System.Data.Linq;
using System.Transactions;
using LinqToSqlExtensions;using System.ServiceModel;
using System.Collections;
using System.Collections.Generic;
using CardsServer.DAL;

namespace CardsServer.WCF
{
	public partial interface IService
{
[OperationContract]
string ClientAddress_Add(ClientAddress model ,int ACID);


[OperationContract]
string ClientAddress_Update(ClientAddress model ,int ACID);


[OperationContract]
string ClientAddressByAddID_Delete(int AddID,int ACID);


[OperationContract]
ClientAddress ClientAddressByAddID_Get(int AddID,int ACID );


[OperationContract]
List<ClientAddress> ClientAddress_Get(string sortBy,int ACID);




}
public partial class Service : IService
{
#region  添加ClientAddress表数据
/// <summary>
/// 添加ClientAddress
/// </summary>
/// <param name="model">ClientAddress实体</param>
/// <returns>空或错误</returns>
public string ClientAddress_Add(ClientAddress model,int ACID)
{
if(model.AddID==null )
{
return "『收货地址编号』不能为空！";
}
if(model.IsDefault==null )
{
return "『缺省地址』不能为空！";
}
if(string.IsNullOrEmpty(model.Tel))
{
return "『收货电话』不能为空！";
}
if(string.IsNullOrEmpty(model.Contact))
{
return "『收货人』不能为空！";
}
if(string.IsNullOrEmpty(model.Address))
{
return "『收货地址』不能为空！";
}
if(string.IsNullOrEmpty(model.ClientCode))
{
return "『客户编号』不能为空！";
}
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if(ctx.ClientAddress.Count(p=>p.AddID==model.AddID)>0)
{
return "主键已存在！";
}
ctx.ClientAddress.InsertOnSubmit(model);
ctx.SubmitChanges();
return string.Empty;
}
catch (Exception ex)
{
return " 添加失败!错误详细信息：" + ex.Message;
}
}
}
#endregion

#region  添加ClientAddress表数据事物用重构
/// <summary>
/// 添加ClientAddress事物用重构
/// </summary>
/// <param name="model">ClientAddress实体</param>
/// <returns>空或错误</returns>
private string ClientAddress_Add(ClientAddress model,int ACID, CardsServer.DAL.DataClassesDataContext ctx)
{
if(model.AddID==null )
{
return "『收货地址编号』不能为空！";
}
if(model.IsDefault==null )
{
return "『缺省地址』不能为空！";
}
if(string.IsNullOrEmpty(model.Tel))
{
return "『收货电话』不能为空！";
}
if(string.IsNullOrEmpty(model.Contact))
{
return "『收货人』不能为空！";
}
if(string.IsNullOrEmpty(model.Address))
{
return "『收货地址』不能为空！";
}
if(string.IsNullOrEmpty(model.ClientCode))
{
return "『客户编号』不能为空！";
}
if(ctx.ClientAddress.Count(p=>p.AddID==model.AddID)>0)
{
return "主键已存在！";
}
ctx.ClientAddress.InsertOnSubmit(model);
return string.Empty;
}
#endregion



#region  修改ClientAddress表数据
/// <summary>
/// 修改ClientAddress
/// </summary>
/// <param name="model">ClientAddress实体</param>
/// <returns>空或错误</returns>
public string ClientAddress_Update(ClientAddress model,int ACID)
{
if(model.AddID==null )
{
return "『收货地址编号』不能为空！";
}
if(model.IsDefault==null )
{
return "『缺省地址』不能为空！";
}
if(string.IsNullOrEmpty(model.Tel))
{
return "『收货电话』不能为空！";
}
if(string.IsNullOrEmpty(model.Contact))
{
return "『收货人』不能为空！";
}
if(string.IsNullOrEmpty(model.Address))
{
return "『收货地址』不能为空！";
}
if(string.IsNullOrEmpty(model.ClientCode))
{
return "『客户编号』不能为空！";
}
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if(ctx.ClientAddress.Count(p=>p.AddID==model.AddID)!= 1)
{
return "未找到指定记录或记录不唯一！";
}
ClientAddress ClientAddressmodel=ctx.ClientAddress.Single(p => p.AddID==model.AddID);
if(model.AddID!=null )
{
ClientAddressmodel.AddID=model.AddID;
}
if(!string.IsNullOrEmpty(model.ClientCode))
{
ClientAddressmodel.ClientCode=model.ClientCode;
}
if(!string.IsNullOrEmpty(model.Address))
{
ClientAddressmodel.Address=model.Address;
}
if(!string.IsNullOrEmpty(model.Contact))
{
ClientAddressmodel.Contact=model.Contact;
}
if(!string.IsNullOrEmpty(model.Tel))
{
ClientAddressmodel.Tel=model.Tel;
}
if(!string.IsNullOrEmpty(model.ZIP))
{
ClientAddressmodel.ZIP=model.ZIP;
}
if(model.IsDefault!=null )
{
ClientAddressmodel.IsDefault=model.IsDefault;
}
ctx.SubmitChanges();
return string.Empty;
}
catch (Exception ex)
{
return " 修改失败!错误详细信息：" + ex.Message;
}
}
}
#endregion



#region  删除ClientAddress表ByAddID数据
/// <summary>
/// 删除ClientAddressByAddID
/// </summary>
/// <param name="AddID">收货地址编号</param>
/// <returns>空或错误</returns>
public string ClientAddressByAddID_Delete(int AddID,int ACID)
{
if(AddID==null )
{
return "『收货地址编号』不能为空！";
}
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if(ctx.ClientAddress.Count(p=>p.AddID==AddID)!=1)
{
return null;
}
ClientAddress model=ctx.ClientAddress.Single(p => p.AddID== AddID);
ctx.ClientAddress.DeleteOnSubmit(model);
ctx.SubmitChanges();
return string.Empty;
}
catch (Exception ex)
{
return " 删除失败!错误详细信息：" + ex.Message;
}
}
}
#endregion

#region  删除ClientAddress表ByAddID数据[事物用重构]
/// <summary>
/// 删除ClientAddressByAddID[事物用重构]
/// </summary>
/// <param name="AddID">收货地址编号</param>
/// <returns>空或错误</returns>
private string ClientAddressByAddID_Delete(int AddID,int ACID, CardsServer.DAL.DataClassesDataContext ctx)
{
if(AddID==null )
{
return "『收货地址编号』不能为空！";
}
if(ctx.ClientAddress.Count(p=>p.AddID==AddID)!=1)
{
return null;
}
ClientAddress model=ctx.ClientAddress.Single(p => p.AddID== AddID);
ctx.ClientAddress.DeleteOnSubmit(model);
return string.Empty;
}
#endregion



#region  获取ClientAddress表ByAddID数据
/// <summary>
/// 获取ClientAddressByAddID
/// </summary>
/// <param name="AddID">收货地址编号</param>
/// <returns>当前表</returns>
public ClientAddress ClientAddressByAddID_Get(int AddID,int ACID)
{
if(AddID==null )
{
return null;
}
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if(ctx.ClientAddress.Count(p=>p.AddID==AddID)!=1)
{
return null;
}
return ctx.ClientAddress.SingleOrDefault(p=>p.AddID==AddID);
}
catch (Exception ex)
{
return null;
}
}
}
#endregion



#region  获取ClientAddress表数据
/// <summary>
/// 获取ClientAddress
/// </summary>
/// <param name="sortBy">排序属性名</param>
/// <param name="sortType">排序类型0=升序 1=降序</param>
/// <returns>List<ClientAddress></returns>
public List<ClientAddress> ClientAddress_Get(Expression<Func<ClientAddress, bool>> predicate , string sortBy,int ACID)
{
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if (predicate == null)
{
if(string.IsNullOrWhiteSpace(sortBy))
{
return ctx.ClientAddress.ToList();
}
 else 
{
return ctx.ClientAddress.OrderBy(sortBy).ToList();
}
}
else
{
if(string.IsNullOrWhiteSpace(sortBy))
{
return ctx.ClientAddress.Where(predicate).ToList();
}
 else 
{
return ctx.ClientAddress.Where(predicate).OrderBy(sortBy).ToList();
}
}
}
catch (Exception ex)
{
return null;
}
}
}
#endregion


#region  获取ClientAddress表数据
/// <summary>
/// 获取ClientAddress
/// </summary>
/// <param name="sortBy">排序属性名</param>
/// <param name="sortType">排序类型0=升序 1=降序</param>
/// <returns>List<ClientAddress></returns>
public List<ClientAddress> ClientAddress_Get(string sortBy,int ACID)
{
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
return ClientAddress_Get(null,sortBy, ACID);
}
catch (Exception)
{
return null;
}
}
}
#endregion


#region  获取ClientAddress表数据计数
/// <summary>
/// 获取ClientAddress表数据计数
/// </summary>
/// <returns>List<ClientAddress></returns>
public int ClientAddress_GetCount(Expression<Func<ClientAddress, bool>> predicate,int ACID)
{
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if (predicate == null)
{
return ctx.ClientAddress.Count();

}
 else
{
return ctx.ClientAddress.Where(predicate).Count();
}
}
catch 
{
return 0;
}
}
}
#endregion




#region  获取ClientAddress分页表数据
/// <summary>
/// 获取ClientAddress分页表数据
/// </summary>
/// <param name="pageIndex">当前页</param>
/// <param name="pageSize">页大小</param>
/// <param name="sortBy">排序属性名</param>
/// <param name="sortType">排序类型0=升序 1=降序</param>
/// <returns>List<ClientAddress></returns>
public List<ClientAddress> ClientAddress_Getpage(int pageIndex, int pageSize,Expression<Func<ClientAddress, bool>> predicate ,string sortBy,int ACID)
{
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if (predicate == null)
{
if(string.IsNullOrWhiteSpace(sortBy))
{
return ctx.ClientAddress.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
}
 else 
{
return ctx.ClientAddress.OrderBy(sortBy).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
}
}
else
{
if(string.IsNullOrWhiteSpace(sortBy))
{
return ctx.ClientAddress.Where(predicate).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
}
 else 
{
return ctx.ClientAddress.Where(predicate).OrderBy(sortBy).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
}
}
}
catch (Exception ex)
{
return null;
}
}
}
#endregion


#region  批量修改ClientAddress表数据
/// <summary>
/// 批量修改ClientAddress
/// </summary>
/// <param name="predicate">Expression<Func<ClientAddress, bool>></param> 查找表达式树
/// <param name="updater">Expression<Func<ClientAddress,ClientAddress>> 更新表达式树</param>
/// <param name="returns">影响记录数</param>
public int ClientAddress_batchUpdate(Expression<Func<ClientAddress, bool>> predicate,Expression<Func<ClientAddress, ClientAddress>> updater,int ACID)
{
using (TransactionScope ts=new TransactionScope())
{
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
int ok = ctx.ClientAddress.Update(predicate, updater);
ts.Complete();
 return ok;
}
catch (Exception ex)
{
return 0;
}
}
}
}
#endregion


#region  批量修改ClientAddress表数据事物用重构
/// <summary>
/// 批量修改ClientAddress事物用重构
/// </summary>
/// <param name="predicate">Expression<Func<ClientAddress, bool>></param> 查找表达式树
/// <param name="updater">Expression<Func<ClientAddress,ClientAddress>> 更新表达式树</param>
/// <param name="ctx">数据上下文</param>
/// <param name="returns">影响记录数</param>
private int ClientAddress_batchUpdate(Expression<Func<ClientAddress, bool>> predicate,Expression<Func<ClientAddress, ClientAddress>> updater,int ACID, CardsServer.DAL.DataClassesDataContext ctx)
{
int ok = ctx.ClientAddress.Update(predicate, updater);
return ok;
}
#endregion


#region  批量删除ClientAddress表数据
/// <summary>
/// 批量删除ClientAddress
/// </summary>
/// <param name="predicate">Expression<Func<ClientAddress, bool>></param> 查找表达式树
/// <param name="returns">影响记录数</param>
public int ClientAddress_batchDel(Expression<Func<ClientAddress, bool>> predicate,int ACID)
{
using (TransactionScope ts=new TransactionScope())
{
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
int ok = ctx.ClientAddress.Delete(predicate);
ts.Complete();
 return ok;
}
catch (Exception ex)
{
return 0;
}
}
}
}
#endregion


#region  批量删除ClientAddress表数据事物用重构
/// <summary>
/// 批量删除ClientAddress事物用重构
/// </summary>
/// <param name="predicate">Expression<Func<ClientAddress, bool>></param> 查找表达式树
/// <param name="ctx">数据上下文</param>
/// <param name="returns">影响记录数</param>
private int ClientAddress_batchDel(Expression<Func<ClientAddress, bool>> predicate,int ACID, CardsServer.DAL.DataClassesDataContext ctx)
{
int ok = ctx.ClientAddress.Delete(predicate);
return ok;
}
#endregion


}
}

